<template>
    <div>
        <el-row class="lock-btn">
            <el-button-group>
                <el-button :disabled="userState" size="medium" type="primary" @click="addBtn" round>添加</el-button>
            </el-button-group>
        </el-row>
        <el-row style="box-shadow: rgb(205 205 205) 1px 1px 8px;">
            <el-col :span="24">
                <el-table :header-cell-style="{ background: '#ecf3fd',color:'#000000' }" highlight-current-row  ref="managerTable" v-loading="loading" :data="managers" :stripe=true :row-key="'id'" :max-height="clientHeight" :height="clientHeight" @selection-change="handleTableChange">
  
                    <el-table-column prop="nickName" label="用户昵称" width="" align="center" sortable>
                    </el-table-column>
                    <el-table-column prop="loginName" label="用户登陆名" width="" align="center" sortable>
                    </el-table-column>
                    <el-table-column prop="phone" label="手机"  align="center"  width="">
                    </el-table-column>
                    <el-table-column prop="roleId" label="用户角色" :formatter="roleIdFormatter" width="120">
                    </el-table-column>
                    <el-table-column prop="createDate" label="创建时间"  align="center" width="">
                    </el-table-column>
                    <el-table-column prop="roleCode" label="roleCode" align="center" width="">
                    </el-table-column>
                    <el-table-column label="操作" width="" align="center" >
                        <template slot-scope="scope">
                            <el-button :disabled="userState" class="operateBtn" size="mini" type="primary" @click="editBtn(scope.$index, scope.row)" title="编辑"><i class="el-icon-edit-outline"></i></el-button>
                            <el-button :disabled="userState" class="operateBtn" size="mini" type="danger" @click="delBtn(scope.$index, scope.row)" title="删除"><i class="el-icon-delete"></i></el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24" class="page-tool">
                <el-pagination layout="total,sizes,prev, pager,next,jumper" :page-size="pageSize" :total="total" :page-sizes="[10, 20, 30, 40, 100]" @size-change="handlePageChange" @current-change="handleNoChange">
                </el-pagination>
            </el-col>
        </el-row>
        <el-dialog :title="dialogTitle" :visible.sync="addDialog" :width="'422px'">
            <el-form :model="form" :rules="rules" ref="form">
                <el-form-item label="用户昵称" :label-width="labelWidth" prop="nickName">
                    <el-input v-model="form.nickName " auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="登陆名" :label-width="labelWidth" v-if="dialogTitle==='添加用户'" prop="loginName">
                    <el-input v-model="form.loginName " auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码 " :label-width="labelWidth" prop="loginPassword">
                    <el-input v-model="form.loginPassword " auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机" :label-width="labelWidth" prop="phone">
                    <el-input v-model="form.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色" :label-width="labelWidth" prop="roleId">
                    <el-select v-model="form.roleId" placeholder="请选角色">
                        <el-option key="1" label="管理员" :value="1"></el-option>
                        <el-option key="2" label="代理商" :value="2"></el-option>
                        <el-option key="3" label="运营商" :value="3"></el-option>
                        <el-option key="4" label="客服" :value="4"></el-option>
                        <el-option key="4" label="电瓶车运营商" :value="5"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" :label-width="labelWidth">
                    <el-input v-model="form.remark" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialog = false">取 消</el-button>
                <el-button type="primary" @click="saveManager">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>  
import {
  loadManagerApi,
  delManagerApi,
  addManagerApi,
  updateManagerApi
} from "../../api/manager.js";
import { getNickName, getTestUserName } from "../../util/auth";
export default {
  name: "runInfo",
  data: function() {
    return {
      clientHeight:
        Math.max(
          document.documentElement.scrollHeight,
          document.body.scrollHeight
        ) -
        132 -
        5 +
        10 -
        28,
      labelWidth: "100px",
      loading: false,
      dialogTitle: "",
      pageNo: 1,
      pageSize: 20,
      total: 0,
      multipleSelection: [],
      managers: [],
      parkfields: [],
      form: {},
      addDialog: false,

      rules: {
        nickName: [
            { required: true, message: '请输入用户昵称', trigger: 'blur' }
          ],
        loginName: [
            { required: true, message: '请输入登陆名', trigger: 'blur' }
          ],
        loginPassword: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
        phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ],
        roleId: [
            { required: true, message: '请选择角色', trigger: 'change' }
          ],
      },
    };
  },
  methods: {
    saveManager() {
      let _this = this;
       _this.$refs.form.validate((valid) => {
        if (valid) {
          if (_this.dialogTitle === "添加用户") {
            addManagerApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadManagers(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "用户添加成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "用户添加失败",
                  type: "error"
                });
              }
            });
          } else {
            updateManagerApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadManagers(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "用户修改成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "用户修改失败",
                  type: "error"
                });
              }
            });
          }
        }
      });
    },
    addBtn() {
      let _this = this;
      // _this.$nextTick(()=>{
      //   if(_this.$refs.form != undefined){
      //     _this.$refs.form.resetFields();
      //   }
      // })
      _this.dialogTitle = "添加用户";
      _this.form = {};
      _this.addDialog = true;
    },
    editBtn(index, row) {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.form != undefined){
          _this.$refs.form.resetFields();
        }
      })
      _this.dialogTitle = "修改用户";
      _this.form = row;
      _this.addDialog = true;
    },
    delBtn(index, row) {
      let _this = this;
      this.$confirm('确认删除用户“' + row.name + '”', '提示', {
        type: 'warning'
      })
      .then(_ =>{
        delManagerApi(row.id).then(resp => {
          if (resp.errmsg === "ok") {
            _this.loadManagers(this.pageNo, this.pageSize);
            _this.$notify({
              title: "成功",
              message: "用户删除成功",
              type: "success"
            });
          }
        });
      })
    },
    roleIdFormatter(row, column) {
      if (row.roleId === 1) {
        return "管理员";
      }else if (row.roleId === 2){
        return "代理商";
      }else if (row.roleId === 3){
        return "汽车运营商";
      }else if (row.roleId === 4){
        return "客服";
      }else if (row.roleId === 5){
        return "电瓶车运营商";
      }else {
        return "";
      }
    },
    handlePageChange(pageSize) {
      this.pageSize = pageSize;
      this.loadManagers(this.pageNo, this.pageSize);
    },
    handleNoChange(pageNo) {
      this.pageNo = pageNo;
      this.loadManagers(this.pageNo, this.pageSize);
    },
    handleTableChange(val) {
      this.multipleSelection = val;
    },
    loadManagers(pageNo, pageSize) {
      let _this = this;
      _this.loading = true;
      loadManagerApi(pageNo, pageSize).then(resp => {
        _this.managers = resp.data.dataList;
        _this.total = resp.data.totalNum;
        _this.loading = false;
      });
    }
  },
  computed: {
    userState () {
      return getNickName() === getTestUserName();
    }
  },
  mounted: function() {
    this.$nextTick(function() {
      this.loadManagers(this.pageNo, this.pageSize);
    });
  }
};
</script>

<style scoped>
.page-tool {
  background: white;
}

.page-tool div {
  text-align: right;
}

.lock-btn {
  margin-bottom: 5px;
  margin-top: -10px;
}
.operateBtn {
  font-size: 18px;
  width: 32px;
  height: 32px;
  padding: 0;
}
</style>